<?php
/**
 * Object Relational Persistence Mapping Class for table: province
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class Province extends ORDataObject {

	/**#@+
	 * Fields of table: province mapped to class members
	 */
	var $province_id	= '';
	var $vi_name		= '';
	var $en_name		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'province';

	/**
	 * Primary Key
	 */
	var $_key = 'province_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'Province';
	const HCM_PROVINCE_ID = 701;
	const HCM_DISTRICT_1_ID = 70101;
	/**
	 * Handle instantiation
	 */
	function Province() {
		parent::ORDataObject();
	}
	
	public function getProvinceList(){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "province_id, $field as name",
			'from' 	=> "$this->_table",
			'groupby' => 'province_id',
			'orderby' => 'name'
			);
		$ds->setup($this->_db,$sql);
		return $ds->toArray('province_id', 'name');
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function getAllProvinces(){
		$ds =& new Datasource_sql();
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		
		$sql = array(
			'cols' 	=> "province_id, $field as name",
			'from' 	=> "$this->_table",
			'orderby' => 'name'
			);
		
		$ds->setup($this->_db,$sql);
		return $ds->toArray();
	}
	
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	//get Province by province_id
	public function getProvince($provinceId){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$sql = array(
			'cols' 	=> "province_id as province_id, $field as name",
			'from' 	=> "$this->_table",
			'where' => "province_id='$provinceId'"
		);
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
			else return $result[0];
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function setData($name){
		$data	=	array(
			'name'	=>	$name
		);
		$this->populateArray($data);
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function addProvince($name){
	    $locales = L10nHelper::getSupportedLocales();
       	$data = array();
       	foreach ( $locales as $lo => $la ) {
       		$lo	=	L10nHelper::changeField($lo, "name");
       		$data[$lo] = $name;
       	}
	    $this->populateArray($data);
	    $this->persist();
		return ;
	}
	
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function updateProvince($name, $provinceId){
		$field		=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$data = array($field => $name);
        $this->setup($provinceId);
        $this->populateArray($data);
        $this->persist();
	}
	
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function checkDuplicateName($name){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$sql = array(
			'cols' 	=> "province_id, $field as name",
			'from' 	=> "$this->_table",
			'where' => "$field='$name'");
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
		
		foreach ( $result as $value ) {
			if (strcmp($name,$value['name']) == 0) {
				
				return true;
			}
		}
		return false;
	}

}
?>
